package sc.prototype.pfs.test.eventnotify;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import junit.framework.Assert;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import sc.prototype.pfs.eventnotify.filter.NotifyConfigFilter;
import sc.prototype.pfs.eventnotify.model.NotifyConfig;
import sc.prototype.pfs.eventnotify.model.enums.EnableFlag;
import sc.prototype.pfs.test.eventnotify.event.MockEvent;
import sc.prototype.pfs.test.eventnotify.event.MockEventContent;
import sc.prototype.pfs.test.eventnotify.event.MockEventContentStatus;

@ContextConfiguration(locations = { "classpath:**/EventNotify-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class NotifyManager_1Filter {

    @Autowired
    NotifyConfigFilter notifyConfigFilter;

    @Before
    public void before() {
        DateTimeUtils.setCurrentMillisFixed(new DateTime(2012, 12, 1, 0, 0).getMillis());
    }

    @After
    public void after() {
        DateTimeUtils.setCurrentMillisSystem();
    }

    public MockEvent createEvent() {
        MockEventContent content = new MockEventContent("str1_c", new BigDecimal("123.45"), "str2_c",
                MockEventContentStatus.SUCCESS);
        MockEvent event = new MockEvent(content);
        return event;
    }

    public NotifyConfig createConfig(String configName) {
        NotifyConfig config = new NotifyConfig();
        config.setConfigName(configName);
        config.setEnableFlag(EnableFlag.Y);
        config.setGmtBeginValid(new DateTime(2012, 12, 1, 0, 0).toDate());
        config.setGmtBeginInvalid(new DateTime(2012, 12, 3, 0, 0).toDate());
        return config;
    }

    public List<NotifyConfig> asList(NotifyConfig... configs) {
        return Arrays.asList(configs);
    }

    @Test
    public void normal_not_valid() {
        NotifyConfig cfg1 = createConfig("configValid");
        NotifyConfig cfg2 = createConfig("configInvalid");
        cfg2.setGmtBeginValid(new DateTime(2012, 12, 2, 0, 0).toDate());
        List<NotifyConfig> rst = notifyConfigFilter.filter(createEvent(), asList(cfg1, cfg2));
        Assert.assertEquals(1, rst.size());
        Assert.assertEquals("configValid", rst.get(0).getConfigName());
    }
}
